package com.zhiyou.app.shop.manager.action;

import java.util.List;

import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.gson.JsonObject;
import com.zhiyou.app.shop.common.Authority;
import com.zhiyou.app.shop.common.BaseAction;
import com.zhiyou.app.shop.common.InnerObject;
import com.zhiyou.app.shop.manager.dao.pojo.UserDetail;
import com.zhiyou.app.shop.manager.service.ISysManageService;

public class UserAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private ISysManageService sysManageService;

	List<UserDetail> listUser;
	
	UserDetail ud = new UserDetail();
	
	String ids;
	
	public UserAction() {
		super();
		this.setCurrentPath(getText("shopManage.path.sysmanage.user"));
	}

	@Authority(0)
	public String userList(){
		try {
			sysManageService.getUsersByPage(this.getInitedPage(), null, Order.desc("createDate"));
		} catch (Exception e) {
			this.setErrorMessage(e);
		}
		return SUCCESS;
	}
	
	@Authority(0)
	public String addUser(){
		return SUCCESS;
	}
	
	@Authority(0)
	public String editUser(){
		this.ud = sysManageService.findUserById(ud.getId());
		return SUCCESS;
	}
	@Authority(0)
	public String saveOrUpdateUser(){
		try {
			Validator.validateUserAdd(ud);
			sysManageService.saveOrUpdateUser(ud);
		} catch (Exception e) {
			this.setErrorMessage(e);
			return ERROR;
		}
		
		return SUCCESS;
	}
	@Authority(0)
	public String deleteUsers(){
		this.printJSON(getResponse(), new InnerObject(){
			public void execute(JsonObject json) {
				sysManageService.deleteUser(ids);
			}
		});
		return null;
	}
	
	public List<UserDetail> getListUser() {
		return listUser;
	}
	public void setListUser(List<UserDetail> listUser) {
		this.listUser = listUser;
	}

	public UserDetail getUd() {
		return ud;
	}

	public void setUd(UserDetail ud) {
		this.ud = ud;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}
	
}
